using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using CommunityServer.Components;
using System.Web.UI.WebControls;
using CommunityServer.Controls;

namespace CommunityServerStuff.Controls
{
    public class PostExtendedAttributesSubForm : WrappedSubFormBase
    {
        #region Child Controls

        private List<TextBox> _textBoxes = null;
        private List<string> _textBoxIds = null;
        private List<string> _extendedAttributeNames = null;

        #endregion

        #region Properties

        public string TextBoxIds
        {
            get { return ((string)ViewState["TextBoxIds"]) ?? string.Empty; }
            set
            {
                ViewState["TextBoxIds"] = value;
                if (!string.IsNullOrEmpty(value))
                {
                    _textBoxIds = new List<string>();
                    foreach (string ea in value.Split(','))
                    {
                        if (!string.IsNullOrEmpty(ea.Trim()))
                            _textBoxIds.Add(ea.Trim());
                    }
                }
            }
        }

        public string ExtendedAttributeNames
        {
            get { return ((string)ViewState["ExtendedAttributeNames"]) ?? string.Empty; }
            set
            {
                ViewState["ExtendedAttributeNames"] = value;
                if (!string.IsNullOrEmpty(value))
                {
                    _extendedAttributeNames = new List<string>();
                    foreach (string ea in value.Split(','))
                    {
                        if (!string.IsNullOrEmpty(ea.Trim()))
                            _extendedAttributeNames.Add(ea.Trim());
                    }
                }
            }
        }

        public override object DataSource
        {
            get
            {
                return base.DataSource;
            }
            set
            {
                if (value is Post || value == null)
                    base.DataSource = value;
                else
                    throw new InvalidCastException("DataSource must be a valid Post object");
            }
        }

        #endregion

        #region SubForm Overrides

        protected override void AttachChildControls()
        {
            if (_textBoxIds == null || _extendedAttributeNames == null || _textBoxIds.Count != _extendedAttributeNames.Count)
                return;

            _textBoxes = new List<TextBox>();
            foreach (string id in _textBoxIds)
            {
                _textBoxes.Add(CSControlUtility.Instance().FindControl(this, id) as TextBox);
            }
        }

        public override void ApplyChangesBeforeCommit(object activeObject)
        {
            if (_textBoxIds == null || _extendedAttributeNames == null || _textBoxIds.Count != _extendedAttributeNames.Count)
                return;

            EnsureChildControls();

            if (!this.IsValid())
                return;

            Post p = activeObject as Post;
            if (p == null)
                return;

            for (int i = 0; i < this._extendedAttributeNames.Count; i++)
            {
                if (this._textBoxes[i] != null)
                    p.SetExtendedAttribute(this._extendedAttributeNames[i], Globals.HtmlEncode(this._textBoxes[i].Text.Trim()));
            }
        }

        #endregion

        #region Data Binding

        public override void DataBind()
        {
            if (_textBoxIds == null || _extendedAttributeNames == null || _textBoxIds.Count != _extendedAttributeNames.Count)
                return;

            base.DataBind();

            Post post = DataSource as Post;
            if (post != null && !Page.IsPostBack)
            {
                for (int i = 0; i < this._extendedAttributeNames.Count; i++)
                {
                    if (this._textBoxes[i] != null)
                        this._textBoxes[i].Text = Globals.HtmlDecode(post.GetExtendedAttribute(this._extendedAttributeNames[i]) ?? "");
                }
            }
        }

        #endregion

        public override bool IsEnabled()
        {
            return true;
        }
    }
}
